<template>
  <el-form
    ref="deptForm"
    :model="form"
    :rules="deptFormRules"
    label-width="120px"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="ele in list"
          :key="ele.id"
          :value="ele.username"
          :label="ele.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { initSimpleList } from '@/api/employees'
import { adddepartments, refDepartments, updateDepartments } from '@/api/department'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    validList: {
      required: true,
      type: Array
    }
  },
  data() {
    /* 部门名称业务校验
       1、同级不允许重复添加相同名称
       2、点击编辑，排除当前这项
    */
    const validName = (rule, value, callback) => {
      // 添加状态
      let validNameList = this.validList.filter(ele => ele.pid === this.pid).map(item => item.name)
      // 编辑状态，排除这一项
      if (this.isEdit) {
        validNameList = this.validList.filter(ele => ele.pid === this.form.pid && ele.id !== this.pid).map(item => item.name)
      }
      console.log(validNameList)
      if (validNameList.includes(value)) {
        callback(new Error(`部门名称：${value}被占用啦`))
      } else {
        callback()
      }
    }
    const validCode = (rule, value, callback) => {
      let validCodeList = this.validList
      // 编辑状态
      if (this.isEdit) {
        validCodeList = this.validList.filter(ele => ele.code !== this.form.code)
        // console.log(validCodeList)
      }
      if (validCodeList.find(ele => ele.code === value)) {
        callback(new Error(`部门编号：${value}被占用啦`))
      } else {
        callback()
      }
    }
    return {
      // 员工列表
      list: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      deptFormRules: {
        name: [
          { required: true, message: '部门名称必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称为1-50位字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码为1-50位字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门管理者必填', trigger: 'blur' }
        ],
        introduce: [
          {
            min: 1,
            max: 300,
            message: '部门介绍为1-300位字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.initList()
    // if(this.isEdit) this.refDepart()
  },
  methods: {
    // 清空表单
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 修改部门详情
    async doEdit() {
      try {
        const res = await updateDepartments(this.form)
        console.log(res)
      } catch (err) {
        console.log(err)
      }
    },
    // 新增部门
    async doAdd() {
      try {
        const data = { ...this.form, pid: this.pid }
        const res = await adddepartments(data)
        // this.$message.success(res.message)
        console.log(res)
      } catch (err) {
        console.log(err)
      }
    },
    // 获取员工简单信息列表
    async initList() {
      try {
        const res = await initSimpleList()
        this.list = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 根据id查询部门详情
    async refDepart() {
      try {
        const res = await refDepartments(this.pid)
        console.log(res)
        this.form = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 表单确定按钮
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
        this.$emit('close')
      })
    },
    // 表单取消按钮
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>
